#########################
# calculating USA ENPP  #
#########################


# usa enpp

#Load data
usa_seat<-read.csv("usa_seatshare.csv")
attach(usa_seat)

#calculating each component
usa_seat$perseat=absseat/totseats
usa_seat$perseat_sq=usa_seat$perseat^2
usa_seat<-na.omit(usa_seat)


##calculate two types of ENPP
#molinar
tb_enpp_np<-
  usa_seat %>%group_by(edate) %>%
  summarise(enpp_np=1+(1/(sum(perseat^2))*((sum(perseat^2)-max(perseat^2))/sum(perseat^2))))

#laakso
tb_enpp<-usa_seat %>%group_by(edate) %>%
  summarise(enpp=1/sum(perseat^2))

#combine two tibbles
enpp_usa<-data.frame(tb_enpp_np,tb_enpp)

#output .csv
write.csv(enpp_usa,"enpp_usa.csv")